<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxOperations = function (nums, k) {
      let arr = nums.sort((a, b) => a - b);
      let count = 0;
      let start = 0, end = nums.length - 1;
      while (start < end) {
        let sum = arr[start] + arr[end]
        if (sum === k) {
          //最开始想的是移除数组 提交时发现超时 改为双指针前进
          start++;
          end--;
          count++;
        } else if (sum < k) {
          start++;
        } else {
          end--;
        }
      }
      return count
    };
    console.log(maxOperations(nums = [1, 2, 3, 4], k = 5));
  </script>
</body>

</html>